package com.example.demo.controller;

import com.example.demo.common.domain.User;
import com.example.demo.service.UserMgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserMgController {

    @Autowired
    private UserMgService userMgService;

    @RequestMapping("/toUserManagement")
    public String ToUserManagement(HttpSession session, Model model)
    {    //要修改设置
        System.out.println("用户名："+session.getAttribute("LoginUser"));

        model.addAttribute("RoleMessage",session.getAttribute("RoleMessage"));
        model.addAttribute("uName",session.getAttribute("LoginUser"));
        model.addAttribute("userList",userMgService.queryAllUser());
        return "UserManagement";
    }

    @RequestMapping("/UserQuery")
    public String UserQuery(User user, Model model,HttpSession session)
    {

        System.out.println(user.getuName()+";"+user.getPhone());
        model.addAttribute("uName",session.getAttribute("LoginUser"));
        model.addAttribute("userList",userMgService.queryUserByNP(user));
        return "UserManagement";
    }
    @RequestMapping("/UsersDelete")
    @ResponseBody
    public String UsersDlete(String uNames){
        System.out.println("用户列表"+uNames);
        String[] uNamelist = uNames.split(",");
        String message= userMgService.deleteByUnames(uNamelist);
        return message;
    }

    @RequestMapping("/addUesr")
    public String AddUesr(User user){
        String message=userMgService.AddUser(user);
        return "redirect:/toUserManagement";
    }



    @RequestMapping("/updateRole")
    public String UpdateRole(User user,HttpSession session){
        String message=userMgService.UpdateRole(user);
            session.setAttribute("RoleMessage",message);
        return "redirect:/toUserManagement";
    }

    @RequestMapping("/Userdelete")
    public String UpdateRole(String uname){
        String message=userMgService.deleteByuser(uname);
        return "redirect:/toUserManagement";
    }
}
